package mf.com.Service.impl.sys;

import jakarta.annotation.Resource;
import mf.com.Service.sys.SysMenuByRoleService;
import mf.com.dto.system.AssginMenuDto;
import mf.com.entity.system.SysMenu;
import mf.com.mapper.sys.SysMenuByRoleMapper;
import mf.com.mapper.sys.SysMenuMapper;
import mf.com.uilts.TreeBuilder;
import mf.com.vo.system.MenuByRoleVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/3/31 9:56
 * Description:  角色菜单关联服务实现层
 */
@Service
public class SysMenuByRoleServiceImpl implements SysMenuByRoleService {
    @Resource
    private SysMenuByRoleMapper menuByRoleMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    /**
     * 根据角色id查询所有菜单以及角色下对应菜单id
     *
     * @param roleId 角色id
     * @return 对应菜单
     */
    @Override
    public MenuByRoleVo selectByRoleId(Long roleId) {
        // 查询角色下所有菜单数量
        final List<Long> menuIds = menuByRoleMapper.selectByRoleId(roleId);
        // 查询所有角色
        final List<SysMenu> menuList = sysMenuMapper.selectAll();

        final MenuByRoleVo menuByRoleVo = new MenuByRoleVo();
        menuByRoleVo.setRoleMenuIds(menuIds);
        menuByRoleVo.setSysMenuList(TreeBuilder.buildTree(menuList));

        return menuByRoleVo;
    }

    /**
     * 重新分配角色id下菜单
     */
    @Override
    public Integer doAssign(AssginMenuDto assginMenuDto) {
        // 删除角色下所有菜单
        menuByRoleMapper.deleteMenuByRoleId(assginMenuDto.getRoleId());
        // 对菜单进行重新分配
        return menuByRoleMapper.doAssign(assginMenuDto);
    }
}
